package net.codeyouth.myblog.user.controller;

import net.codeyouth.myblog.user.bean.User;
import net.codeyouth.myblog.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author Frank create on 2016/1/31 17:19
 */
@Controller
@RequestMapping("/user")
@EnableAutoConfiguration
public class UserController {

  @Autowired
  UserService service;

  @RequestMapping(value = "/insert", method = RequestMethod.POST)
  @ResponseStatus(HttpStatus.CREATED)
  public String insert(@RequestBody User user) {
    int result = service.insert(user);
    if (result == 1) {
      return "Success";
    }
    return "Failed";
  }

  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public User get(@PathVariable("id") int id) {
    User user = service.getByPrimaryKey(id);
    if (user == null) {
      throw new RuntimeException("请求参数错误！");
    }
    return user;
  }

}
